package yjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yjh.mapper.MistakesTopicMapper;
import yjh.mapper.TopicTableMapper;
import yjh.pojo.MistakesTopic;
import yjh.pojo.TopicTable;
import yjh.util.ResultData;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class MistakesTopicServiceImpl implements MistakesTopicService{

    @Autowired
    public TopicTableMapper topicTableMapper;

    @Autowired
    public MistakesTopicMapper mistakesTopicMapper;

    //增加错题
    public String addMistakesTopic(String topic){
        /**
         * TODO
         * 1. 查询该题id
         * 2. 查询是否存在该错题
         * 3. 将错题id、错题次数以及最晚复习时间写入错题本
         */

        System.out.println(topic);
        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(topic);

        if (topicTable == null) return "题目不存在";

        Integer tid = topicTable.getId();
        MistakesTopic mistakesTopic = mistakesTopicMapper.selectMistakesTopicByTopicID(tid);


        if (mistakesTopic != null) {
            int mistakescount = mistakesTopic.getMistakescount();
            mistakesTopicMapper.updateMistakesTopicByTopic(topic,mistakescount+1,new Date());

            return null;
        }

        boolean b = mistakesTopicMapper.insertMistakesTopic(tid, 0, new Date());
        if (!b) return "增加错题失败";

        return null;
    }

    //删除错题
    public String deleteMistakesTopic(String topic){



        boolean b = mistakesTopicMapper.deleteMistakesTopic(topic);



        if (!b) return "删除失败";

        return null;
    }

    //修改错题
    public String updateMistakesTopic(String topic,int status){
        TopicTable topicTable = topicTableMapper.selectTopicTableOneByTopic(topic);
        if (topicTable == null) return "题目不存在";

        Integer tid = topicTable.getId();
        MistakesTopic mistakesTopic = mistakesTopicMapper.
                selectMistakesTopicByTopicID(tid);

        if (mistakesTopic == null) return "该题未在错题本中，请先添加";

        int mistakescount = mistakesTopic.getMistakescount();

        boolean b;
        if (status==1) {
             b = mistakesTopicMapper.
                    updateMistakesTopicByTopic(topic, mistakescount + 1, new Date());
        }else if (status==-1){
            b = mistakesTopicMapper.
                    updateMistakesTopicByTopic(topic, mistakescount - 1, new Date());
        }else return "输入的错题状态不合法";
        if (!b) return "修改失败";

        return null;
    }

    //查询全部错题
    public List<MistakesTopic> selectMistakesTopicAll(){

        List<MistakesTopic> mistakesTopics = mistakesTopicMapper.
                selectMistakesTopicAll();

        if (mistakesTopics==null) return null;

        mistakesTopics.forEach((m) ->{
            Integer tid = m.getTopicid();
            TopicTable topicTable = topicTableMapper.selectTopicTableById(tid);

            m.setTt(topicTable);
        });

        return mistakesTopics;
    }

    //根据重要度查询错题
    public List<MistakesTopic> selectMistakesTopicByImportanc(int importanc){

        List<MistakesTopic> mistakesTopics = mistakesTopicMapper.
                selectMistakesTopicAll();

        if (mistakesTopics==null) return null;

        mistakesTopics.forEach((m) ->{
            Integer tid = m.getTopicid();
            TopicTable topicTable = topicTableMapper.
                    selectTopicTableByIdAndImportanc(tid,importanc);

            System.out.println();

            m.setTt(topicTable);
        });


        return mistakesTopics;
    }

    // 查询指定时间段未复习的错题
    public List<MistakesTopic> selectMistakesTopicByDate(int date){
        if (date<0) return null;

        List<MistakesTopic> mistakesTopics =
                mistakesTopicMapper.selectMistakesTopicByDate(date);

        if (mistakesTopics == null) return null;
        mistakesTopics.forEach((mistakesTopic)->{
            Integer tid = mistakesTopic.getTopicid();

            TopicTable topicTable = topicTableMapper.selectTopicTableById(tid);

            if (topicTable != null) mistakesTopic.setTt(topicTable);
        });

        return mistakesTopics;
    }

    //查询出错次数在多大范围内的错题
    public List<MistakesTopic> selectMistakesTopicByMistakescount(int count1){
        //最大出错次数
        int count2 = selectMistakesTopicMax();

        //查询指定出错次数错题
        List<MistakesTopic> mistakesTopics = mistakesTopicMapper.selectMistakesTopicByMistakescount(count1, count2);

        if (mistakesTopics == null) return null;

        mistakesTopics.forEach((mistakesTopic)->{
            Integer tid = mistakesTopic.getTopicid();

            TopicTable topicTable = topicTableMapper.selectTopicTableById(tid);

            if (topicTable != null)  mistakesTopic.setTt(topicTable);
        });

        return mistakesTopics;
    }

    //查询最多的错误次数
    public int selectMistakesTopicMax(){
        return mistakesTopicMapper.selectMistakesTopicMax();
    }

    //查询id
    public Integer selectMistakesTopicByTopicId(Integer tid){
        MistakesTopic mistakesTopic = mistakesTopicMapper.selectMistakesTopicByTopicID(tid);

        return mistakesTopic.getId();
    }
}
